#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string, int> m;
        for (int i = 0; i < words.size(); i++)
        {
            m[words[i]]++;
        }
        vector<pair<string, int>> tmp(m.begin(), m.end());
        sort(tmp.begin(), tmp.end(), [&](pair<string, int> a, pair<string, int> b)
            {
                if (a.second > b.second) return true;
                if (a.second == b.second)
                    if (a.first < b.first) return true;
                return false;
            });
        vector<string> ret;
        for (int i = 0; i < k; i++)
        {
            ret.push_back(tmp[i].first);
        }
        return ret;
    }
};